package com.taptech.doufu.ui.adapter;

import android.view.LayoutInflater;

import com.taptech.doufu.app.WeMediaApplication;

import java.util.LinkedList;
import java.util.List;

/**
 * 
 <p>
 * Company: DiaoBao
 * </p>
 * 
 * 
 @author huangzh
 * 
 * 
 @date 2014-3-18
 */
public abstract class BaseListAdapter extends TTBaseAdapter {

	private List<Object> dataSource;
	private static LayoutInflater layoutInflater;
	protected boolean mHasHeaderView;
	private List<Object> topSource;
	public void addHeaderView(boolean headFlag) {
		this.mHasHeaderView = headFlag;
	}

	public boolean isAddHeaderView() {
		return mHasHeaderView ;
	}

	@Override
	public int getCount() {
		return isDataSourceNull()? dataSource.size()+getTopCount() : 0;
	}
    
	@Override
	public Object getItem(int position) {
		return isDataSourceNull() ? getObjectData(position):null ;
	}
    private boolean isDataSourceNull(){
    	return (dataSource != null);
    }
    public boolean  existSource(int  position){
    	return isDataSourceNull()? dataSource.size()>position:false;
    }
    public Object getObjectData(int position){
    	return existSource(position)?dataSource.get(position):null;
    }
	@Override
	public long getItemId(int position) {
		return position;
	}
	public List<Object> getDataSource() {
		return dataSource;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setDataSource(List dataSource) {
		this.dataSource = dataSource;
		notifyDataSetInvalidated();
		// notifyDataSetChanged();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void addMoreDatas(List dataSource) {
		if(dataSource==null)
			dataSource = new LinkedList<Object>();		
		dataSource.addAll(dataSource);
		notifyDataSetChanged();
	}

	public void addMoreData(Object data) {
		if(dataSource==null)
			dataSource = new LinkedList<Object>();		
		dataSource.add(data);
		notifyDataSetChanged();
	}

	public void addRefreshData(Object data) {
		if(dataSource==null)
			dataSource = new LinkedList<Object>();		
		//		dataSource.add(0, data);	
		dataSource.add(data);	
		notifyDataSetChanged();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void addRefreshDatas(List datas) {
		if(dataSource==null)
			dataSource = new LinkedList<Object>();		
		dataSource.addAll(0, datas);		
		notifyDataSetChanged();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setSuperDatas(List dataSource) {
		if (this.dataSource == null) {
			this.dataSource = dataSource;
		} else {
			this.dataSource.addAll(dataSource);
		}
		notifyDataSetChanged();
	}
	public void setSuperDatas2(List<?> dataSource) {
		if (this.dataSource == null) {
			this.dataSource = new LinkedList<Object>();
		}  
		this.dataSource.addAll(dataSource);
		notifyDataSetChanged();
	}
	public LayoutInflater getLayoutInflater() {
		if (layoutInflater == null)
			layoutInflater = LayoutInflater
			.from(WeMediaApplication.applicationContext);
		return layoutInflater;
	}


	public void clearDateSource() {
		if (this.dataSource != null) {
			this.dataSource.clear();
			notifyDataSetChanged();
			clear();
		}
	}
	public void setTopSource(List topSource ){
		this.topSource=topSource;
	}
	public List<Object> getTopSource(){
		return topSource;
	}
	public void cleanTopSource(){
		if (this.topSource != null) {
			this.topSource.clear();
			clear();
		}
	}
	protected int getTopCount(){
		return topSource!=null?1:0;
	}
}
